#include <jni.h>
#include <string>
#include "LogUil.h"
#include <cstring>
#include <cstdio>
#include "FFmpegMediaPlayer.h"
#include "player/render/audio/AudioGLRender.h"


extern "C" {
#include <libavcodec/version.h>
#include <libavcodec/avcodec.h>
#include <libavfilter/version.h>
#include <libavformat/version.h>
#include <libavutil/version.h>
#include <libswresample/version.h>
#include <libswscale/version.h>
}


#ifdef __cplusplus
extern "C" {
#endif

jstring
Java_com_nature_ffmpegforandroid_NativeUtils_stringFromJNI(
        JNIEnv *env,
        jobject thiz) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

jstring
Java_com_nature_ffmpegforandroid_NativeUtils_getFFmpegVersion(JNIEnv *env, jobject thiz) {

    char strBuffer[1024 * 4] = {0};

    strcat(strBuffer, "libavcodec->");
    strcat(strBuffer, AV_STRINGIFY(LIBAVCODEC_VERSION));
    strcat(strBuffer, "\nlibavfilter->");
    strcat(strBuffer, AV_STRINGIFY(LIBAVFILTER_VERSION));
    strcat(strBuffer, "\nlibavformat->");
    strcat(strBuffer, AV_STRINGIFY(LIBAVFORMAT_VERSION));
    strcat(strBuffer, "\nlibavutil->");
    strcat(strBuffer, AV_STRINGIFY(LIBAVUTIL_VERSION));
    strcat(strBuffer, "\nlibswresample->");
    strcat(strBuffer, AV_STRINGIFY(LIBSWRESAMPLE_VERSION));
    strcat(strBuffer, "\nlibswscale-->");
    strcat(strBuffer, AV_STRINGIFY(LIBSWSCALE_VERSION));
    LOG_E("%s", strBuffer);
    strcat(strBuffer, "\n\n编译配置 configure -->");
    strcat(strBuffer, avcodec_configuration());

    return env->NewStringUTF(strBuffer);


}


JNIEXPORT jlong JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1init(JNIEnv *env,
                                                                jobject thiz,
                                                                jstring url,
                                                                jint video_render_type,
                                                                jobject surface) {

    const char *nUrl = env->GetStringUTFChars(url, nullptr);
    auto *player = new FFmpegMediaPlayer();
    player->Init(env, thiz, const_cast<char *>(nUrl), video_render_type, surface);
    env->ReleaseStringUTFChars(url, nUrl);
    return reinterpret_cast<jlong>( player);
}


JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1play(JNIEnv *env,
                                                                jobject thiz,
                                                                jlong m_native_play_handle) {
    if (m_native_play_handle != 0) {
        auto *mediaPlayer = reinterpret_cast< FFmpegMediaPlayer * > (m_native_play_handle);
        mediaPlayer->Play();
    }
}


JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1pause(JNIEnv *env,
                                                                 jobject thiz,
                                                                 jlong m_native_play_handle) {
    if (m_native_play_handle != 0) {
        auto *mediaPlayer = reinterpret_cast<FFmpegMediaPlayer *> (m_native_play_handle);
        mediaPlayer->Pause();
    }
}


JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1seek2_1position(JNIEnv *env,
                                                                           jobject thiz,
                                                                           jlong m_native_play_handle,
                                                                           jfloat position) {
    if (m_native_play_handle != 0) {
        auto *mediaPlayer = reinterpret_cast<FFmpegMediaPlayer *>(m_native_play_handle);
        mediaPlayer->SeekToPosition(position);
    }
}


JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1stop(JNIEnv *env,
                                                                jobject thiz,
                                                                jlong m_native_play_handle) {
    if (m_native_play_handle != 0) {
        auto *mediaPlayer = reinterpret_cast<FFmpegMediaPlayer *>(m_native_play_handle);
        mediaPlayer->Stop();
    }
}

JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1un_1init(JNIEnv *env,
                                                                    jobject thiz,
                                                                    jlong m_native_play_handle) {
    if (m_native_play_handle != 0) {
        auto *mediaPlayer = reinterpret_cast<FFmpegMediaPlayer *>(m_native_play_handle);
        mediaPlayer->UnInit();
    }
}

JNIEXPORT jlong JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1get_1media_1param(JNIEnv *env,
                                                                             jobject thiz,
                                                                             jlong m_native_play_handle,
                                                                             jint param_type) {
    long value = 0;
    if (m_native_play_handle != 0) {
        auto *mediaPlayer = reinterpret_cast<FFmpegMediaPlayer *>(m_native_play_handle);
        value = mediaPlayer->GetMediaParams(param_type);
    }
    return value;
}



JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1onsurface_1created(JNIEnv *env,
                                                                              jobject thiz,
                                                                              jint render_type) {
    //TODO
    switch (render_type) {
        case VIDEO_GL_RENDER:

            break;
        case AUDIO_GL_RENDER:
            AudioGLRender::GetInstance()->OnSurfaceCreated();
            break;
        case VR_3D_GL_RENDER:
            // TODO
            break;
        default:
            break;
    }

}

JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1ondraw_1frame(JNIEnv *env,
                                                                         jobject thiz,
                                                                         jint render_type) {
    //TODO
    switch (render_type) {
        case VIDEO_GL_RENDER:

            break;
        case AUDIO_GL_RENDER:
            AudioGLRender::GetInstance()->OnDrawFrame();
            break;
        case VR_3D_GL_RENDER:
            // TODO
            break;
        default:
            break;
    }
}

JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_native_1onsurface_1onchanged(JNIEnv *env,
                                                                                jobject thiz,
                                                                                jint render_type,
                                                                                jint width,
                                                                                jint height) {
    //TODO
    switch (render_type) {
        case VIDEO_GL_RENDER:

            break;
        case AUDIO_GL_RENDER:
            AudioGLRender::GetInstance()->OnSurfaceChanged(width, height);
            break;
        case VR_3D_GL_RENDER:
            // TODO
            break;
        default:
            break;
    }
}


JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_00024Companion_native_1set_1gesture(JNIEnv *env,
                                                                                       jobject thiz,
                                                                                       jint render_type,
                                                                                       jfloat x,
                                                                                       jfloat y,
                                                                                       jint scale) {
    //TODO
    switch (render_type) {
        case VIDEO_GL_RENDER:

            break;
        case AUDIO_GL_RENDER:
            AudioGLRender::GetInstance()->UpdateMVPMatrix(x, y, scale, scale);
            break;
        case VR_3D_GL_RENDER:
            // TODO
            break;
        default:
            break;
    }


}

JNIEXPORT void JNICALL
Java_com_nature_ffmpegforandroid_NatureMediaPlayer_00024Companion_native_1set_1touchloc(JNIEnv *env,
                                                                                        jobject thiz,
                                                                                        jint render_type,
                                                                                        jfloat touch_x,
                                                                                        jfloat touch_y) {
    //TODO
    switch (render_type) {
        case VIDEO_GL_RENDER:

            break;
        case AUDIO_GL_RENDER:
            AudioGLRender::GetInstance()->SetTouchLoc(touch_x, touch_y);
            break;
        case VR_3D_GL_RENDER:
            // TODO
            break;
        default:
            break;
    }

}

#ifdef __cplusplus
}
#endif

